package com.phiture.erp.sale.web.controller.api;

import cn.hutool.core.collection.CollUtil;
import com.phiture.erp.sale.api.pojo.req.ErpSaleOutReq;
import com.phiture.erp.sale.api.pojo.resp.ErpSaleOutItemsResp;
import com.phiture.erp.sale.api.pojo.resp.ErpSaleOutResp;
import com.phiture.erp.sale.api.service.ErpSaleOutApi;
import com.phiture.erp.sale.core.service.ErpSaleOutService;
import com.phiture.erp.sale.dal.entity.ErpSaleOutDO;
import com.phiture.erp.sale.dal.entity.ErpSaleOutItemDO;
import com.phiture.erp.sale.web.controller.convert.ErpSaleOutConvertMapper;
import javafx.util.Pair;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author YH
 * @date 2025-06-16-16:48
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class ErpSaleOutApiServiceImpl implements ErpSaleOutApi {
    private final ErpSaleOutService erpSaleOutService;

    @Override
    public List<ErpSaleOutResp> getErpSaleOutList(ErpSaleOutReq req) {
        List<ErpSaleOutDO> list = erpSaleOutService.getSaleOutList(req.getStatus(), req.getStartDate(), req.getEndDate());
        return ErpSaleOutConvertMapper.INSTANCE.toErpSaleOutRespList(list);
    }

    @Override
    public List<ErpSaleOutItemsResp> getErpSaleOutItemList(Map<Long, javafx.util.Pair<String, Long>> saleOutMap) {
        List<ErpSaleOutItemDO> items = erpSaleOutService.getSaleOutItemListByOutIds(saleOutMap.keySet());
        if(CollUtil.isEmpty(items)){
            return Collections.emptyList();
        }
        return items.stream().map(item ->{
            ErpSaleOutItemsResp resp = new ErpSaleOutItemsResp();
            resp.setSaleOutId(item.getOutId());
            Pair<String, Long> pair = saleOutMap.get(item.getOutId());
            if(pair != null){
                resp.setSaleOutNo(pair.getKey());
                resp.setCustomerId(pair.getValue());
            }
            resp.setSaleOutItemId(item.getId());
            resp.setWarehouseId(item.getWarehouseId());
            resp.setMaterialId(item.getMaterialId());
            resp.setMaterialUnitId(item.getMaterialUnitId());
            resp.setMaterialPrice(item.getMaterialPrice());
            resp.setCount(item.getCount());
            resp.setTotalPrice(item.getTotalPrice());
            resp.setTaxPercent(item.getTaxPercent());
            resp.setTaxPrice(item.getTaxPrice());
            return resp;
        }).collect(Collectors.toList());
    }

    @Override
    public void backfillSaleOutAccountCheckFlag(Collection<Long> saleOutIds, Boolean accountCheckFlag) {
        erpSaleOutService.backfillSaleOutAccountCheckFlag(saleOutIds, accountCheckFlag);
    }

    @Override
    public void updateSaleOutAccountCheckFlag(Collection<String> saleOutNos, Boolean accountCheckFlag) {
        erpSaleOutService.updateSaleOutAccountCheckFlag(saleOutNos, accountCheckFlag);
    }
}
